﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.IO;
using System.Windows.Forms;
using DAL;
using Entities;

namespace BL
{
    public class ClientesBLL 
    {
        public DataSet dt;
        public DataTable tblClientes;

        public static DataSet CrearDataset()
        {
           // string strFilePath = Application.StartupPath + "\\Datasets_xml\\";
            string strFilePath = Application.StartupPath + "\\Datasets_xml\\";
            if (File.Exists(strFilePath + "Clientes.xml"))
            {
                DataSet dt = new DataSet();
                dt.ReadXml(strFilePath + "Clientes.xml");
                dt.AcceptChanges();
                return dt;
            }
            else
            {
                DataSet dt = DAL.ClientesDAL.CrearDataset();
                dt.Tables["Clientes"].WriteXml(strFilePath + "Clientes.xml", XmlWriteMode.WriteSchema);
                return dt;
            }
        }

        public static DataTable Buscar(DataTable tabla, TextBox txtParametros)
        {
            DataTable buscados = DAL.ClientesDAL.Buscar(tabla, txtParametros);
            return buscados;
        }

        public static void InsertarDT(DataTable tabla, Clientes entidad)
        {
            DAL.ClientesDAL.InsertarDT(tabla, entidad);
        }

        public static void EditarDT(DataRowView vistaFilaActual, Clientes entidad)
        {
            DAL.ClientesDAL.EditarDT(vistaFilaActual, entidad);
        }

        public static void EditarDT(string idCliente, Clientes entidad, DataTable tbl, DataSet dt)
        {
            DAL.ClientesDAL.EditarDT(idCliente, tbl, entidad, dt);
        }

        public static void BorrarDT(DataGridView gvwDatos, string origen, DataSet dt)
        {
            DAL.ClientesDAL.BorrarDT(gvwDatos, origen, dt);
        }

        public static void GrabarDB(DataSet dt)
        {
            MySqlTransaction tr = null;
            try
            {
                MySqlConnection SqlConnection1 = DALBase.GetConnection();
                SqlConnection1.Open();
                tr = SqlConnection1.BeginTransaction();
                DAL.ClientesDAL.GrabarDB(dt, SqlConnection1, tr);
             //   string strFilePath = Application.StartupPath + "\\Datasets_xml\\";
                string strFilePath = Application.StartupPath + "\\Datasets_xml\\";
                dt.Tables["Clientes"].WriteXml(strFilePath + "Clientes.xml", XmlWriteMode.WriteSchema);
                tr.Commit();
                SqlConnection1.Close();
            }
            catch (MySqlException ex)
            {
                MessageBox.Show(ex.ToString(), "NcSoft", MessageBoxButtons.OK, MessageBoxIcon.Information);
                dt.RejectChanges();
                tr.Rollback();
            }
        }
    }
}
